package cn.s1995.sloth.bizs.event.listener;

import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.NumberUtil;
import cn.s1995.sloth.common.core.constant.enums.hint.Hint;
import cn.s1995.sloth.bizs.entity.Prod;
import cn.s1995.sloth.bizs.entity.Sku;
import cn.s1995.sloth.bizs.event.ConfirmOrderEvent;
import cn.s1995.sloth.bizs.event.order.ConfirmOrderOrder;
import cn.s1995.sloth.bizs.exception.BizException;
import cn.s1995.sloth.bizs.oo.dto.ShopCartItemDTO;
import cn.s1995.sloth.bizs.oo.dto.ShopCartOrderDTO;
import cn.s1995.sloth.bizs.service.ProdService;
import cn.s1995.sloth.bizs.service.SkuService;
import cn.s1995.sloth.bizs.service.UserAddressService;
import lombok.AllArgsConstructor;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Objects;

/**
 * ConfirmOrderListener
 *
 * @author Lex
 * @description 确认订单监听器
 * @date 2024/01/02 14:17
 */
@Component
@AllArgsConstructor
public class ConfirmOrderListener {
    private final UserAddressService userAddressService;

    // private final TransportManagerService transportManagerService;

    private final ProdService prodService;

    private final SkuService skuService;

    /**
     * 计算订单金额
     */
    @EventListener(ConfirmOrderEvent.class)
    @Order(ConfirmOrderOrder.DEFAULT)
    public void defaultConfirmOrderEvent(ConfirmOrderEvent event) {

        ShopCartOrderDTO shopCartOrderDto = event.getShopCartOrderDto();

        // OrderREQ orderParam = event.getOrderParam();
        // Long userId = SecurityUtils.getUser().getId();
        // // 订单的地址信息
        // UserAddress userAddr = userAddressService.getUserAddrByUserId(orderParam.getAddrId(), userId);

        BigDecimal total = BigDecimal.ZERO;

        int totalCount = 0;

        BigDecimal transFee = BigDecimal.ZERO;

        for (ShopCartItemDTO shopCartItem : event.getShopCartItems()) {
            // 获取商品信息
            Prod product = prodService.getProduct(shopCartItem.getProdId());
            // 获取sku信息
            Sku sku = skuService.getSku(shopCartItem.getSkuId());
            if (Objects.isNull(product) || Objects.isNull(sku)) {
                throw new BizException(Hint.APP_CONTAINS_UNRECOGNIZED_PRODUCTS);
            }
            if (BooleanUtil.isFalse(product.getIsSale()) || BooleanUtil.isFalse(sku.getIsSale())) {
                throw new BizException(Hint.APP_CONTAINS_UNRECOGNIZED_PRODUCTS, sku.getProdName());
            }

            totalCount = shopCartItem.getProdNum() + totalCount;
            total = NumberUtil.add(shopCartItem.getTotalAmount(), total);
            // 用户地址如果为空，则表示该用户从未设置过任何地址相关信息
            // if (Objects.nonNull(userAddr)) {
            //     // 每个产品的运费相加
            //     transFee = Arith.add(transFee, transportManagerService.calculateTransfee(shopCartItem, userAddr));
            // }

            shopCartItem.setActualTotal(shopCartItem.getTotalAmount());
            shopCartOrderDto.setActualTotal(NumberUtil.add(total, transFee));
            shopCartOrderDto.setTotal(total);
            shopCartOrderDto.setTotalCount(totalCount);
            shopCartOrderDto.setTransfee(transFee);
        }
    }

}
